package com.emperorque.mathpractice.view;

import com.emperorque.mathpractice.model.Expression;

import java.io.PrintStream;
import java.util.List;

/**
 * 控制台表达式打印器实现类
 */
public class ConsoleExpressionPrinter implements ExpressionPrinter {
    
    private PrintStream out;
    
    public ConsoleExpressionPrinter() {
        this.out = System.out;
    }
    
    @Override
    public void setOutputStream(PrintStream out) {
        if (out == null) {
            throw new IllegalArgumentException("输出流不能为空");
        }
        this.out = out;
    }
    
    @Override
    public void print(List<Expression> expressions, String title) {
        if (expressions == null) {
            throw new IllegalArgumentException("算式列表不能为空");
        }
        
        out.println("=".repeat(50));
        out.println(title != null ? title : "算式练习题");
        out.println("=".repeat(50));
        out.println();
        
        for (int i = 0; i < expressions.size(); i++) {
            Expression expr = expressions.get(i);
            out.printf("%2d. %s = ____%n", i + 1, expr.getExpressionString());
        }
        
        out.println();
        out.println("-".repeat(50));
        out.println("答案：");
        out.println("-".repeat(50));
        
        for (int i = 0; i < expressions.size(); i++) {
            Expression expr = expressions.get(i);
            out.printf("%2d. %s%n", i + 1, expr.toString());
        }
        
        out.println("=".repeat(50));
        out.println();
    }
}
